package com.kla.power.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kla.power.entity.*;
import com.kla.power.mapper.*;
import com.kla.power.service.IDeviceService;
import com.kla.power.service.IInverterElectricService;
import com.kla.power.util.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 设备：逆变器、汇流箱、电表、组件、微逆、机器人、无人机 服务实现类
 * </p>
 * @author WuZH
 * @since 2023-06-23
 */
@Service
public class DeviceServiceImpl extends ServiceImpl<DeviceMapper, DeviceEntity> implements IDeviceService {

    @Resource
    private DeviceMapper deviceMapper;
    @Resource
    private InverterAcAmountMapper inverterAcAmountMapper;
    @Resource
    private InverterDcAmountMapper inverterDcAmountMapper;
    @Resource
    private InverterElectricMapper inverterElectricMapper;
    @Resource
    private InverterAmountStatisticsDayMapper inverterAmountStatisticsDayMapper;

    /**
     * 综合查询所有设备
     * @param deviceEntity
     * @return
     */
    @Override
    public List<DeviceEntity> queryList (DeviceEntity deviceEntity) {
        deviceEntity.setPage ( ( deviceEntity.getPage () - 1 ) * deviceEntity.getLimit () );
        List<DeviceEntity> deviceEntities = null;
        if ( deviceEntity.getDeviceType () == ConstUtil.DEVICE_TYPE_INVERTER ) {
            deviceEntities = deviceMapper.queryList ( deviceEntity );
            //处理实时数据
            deviceEntities.forEach ( device -> {
                device.setGridPower ( CommonUtil.parseDecimalPoint ( Double.parseDouble ( StringUtils.isBlank ( device.getGridPower () ) ? "0" : device.getGridPower () ) / 1000 ) );
                String full = CommonUtil.divided ( Double.parseDouble ( StringUtils.isBlank ( device.getTodayEnergy () ) ? "0" : device.getTodayEnergy () ), Double.parseDouble ( StringUtils.isBlank ( device.getPower () ) ? "0" : device.getPower () ) );
                device.setFullHour ( full ); //满发小时=发电量/装机容量
                String v = CommonUtil.divided ( Double.parseDouble ( StringUtils.isBlank ( device.getGridPower () ) ? "0" : device.getGridPower () ), Double.parseDouble ( StringUtils.isBlank ( device.getPower () ) ? "0" : device.getPower () ) );
                device.setAvgActivePower ( v ); //计算公式：功率归一化=当前发电功率/装机容量
            } );
        } else if ( deviceEntity.getDeviceType () == ConstUtil.DEVICE_TYPE_COLLECTOR ) {
            deviceEntities = deviceMapper.queryCollectionList ( deviceEntity );
        }
        return deviceEntities;
    }

    @Override
    public DeviceEntity updateSingle (DeviceEntity deviceEntity) {
        UpdateWrapper<DeviceEntity> updateWrapper = new UpdateWrapper<> ();
        if ( StringUtils.isNotBlank ( deviceEntity.getDeviceTitle () ) ) {
            updateWrapper.set ( "deviceTitle", deviceEntity.getDeviceTitle () );
        }
        if ( StringUtils.isNotBlank ( deviceEntity.getStationId () ) ) {
            updateWrapper.set ( "stationId", deviceEntity.getStationId () );
        }
        if ( StringUtils.isNotBlank ( deviceEntity.getAddress () ) ) {
            updateWrapper.set ( "address", deviceEntity.getAddress ().replace ( " ", "" ).trim () );
        }
        if ( StringUtils.isNotBlank ( deviceEntity.getPower () ) ) {
            updateWrapper.set ( "power", deviceEntity.getPower ().replace ( " ", "" ).trim () );
        }
        updateWrapper.eq ( "id", deviceEntity.getId () );
        int update = baseMapper.update ( null, updateWrapper );
        if ( update == 0 ) {
            throw new StateException ( StateEnum.UPDATE_EXCE );
        }
        return deviceEntity;
    }


    @Override
    public Integer queryCount (DeviceEntity deviceEntity) {
        if ( deviceEntity == null ) {
            throw new StateException ( StateEnum.REQUEST_PARAM_EXCE );
        }
        // 今天所在的分区名称
        LocalDate localDate = LocalDate.now ();
        //String yearAndMonth = localDate.getYear () + "" + ( localDate.getMonthValue () < 10 ? "0" + localDate.getMonthValue () : localDate.getMonthValue () );
        //String partitionName = "partition (p" + yearAndMonth + ")";
        //deviceEntity.setPartitionName ( partitionName );
        Integer integer = 0;
        if ( deviceEntity.getDeviceType () == ConstUtil.DEVICE_TYPE_INVERTER ) {
            integer = deviceMapper.queryCount ( deviceEntity );
        } else if ( deviceEntity.getDeviceType () == ConstUtil.DEVICE_TYPE_COLLECTOR ) {
            integer = deviceMapper.queryCollectionCount ( deviceEntity );
        }
        return integer;
    }


    @Override
    public DeviceEntity saveSingle (DeviceEntity deviceEntity) {
        if ( deviceEntity == null ) {
            throw new StateException ( StateEnum.REQUEST_PARAM_EXCE );
        }
        if ( StringUtils.isBlank ( deviceEntity.getDeviceSn () ) ) {
            throw new StateException ( StateEnum.PRIMARY_NULL_EXCE );
        }
        deviceEntity.setId ( SnowflakeIdWorker.getInstance ().nextStringId () );
        deviceEntity.setDeviceSn ( deviceEntity.getDeviceSn ().trim ().replace ( " ", "" ) );
        deviceEntity.setCreateTime ( new Date () );
        // 去枚举中拿标题
        String title = DeviceEnum.getTitle ( deviceEntity.getDeviceType () );
        deviceEntity.setDeviceTitle ( title );
        int insert = baseMapper.insert ( deviceEntity );
        if ( insert == 0 ) {
            throw new StateException ( StateEnum.INSERT_EXCE );
        }
        return deviceEntity;
    }

    @Override
    public DeviceEntity querySingle (DeviceEntity deviceEntity) {
        if ( deviceEntity == null ) {
            throw new StateException ( StateEnum.REQUEST_PARAM_EXCE );
        }
        DeviceEntity device = deviceMapper.querySingle ( deviceEntity );
        return device;
    }

    /**
     * 查询设备详情页连接信息
     * 1、先查询该设备
     * 2、通过该类型查看是否是数采，如果是数采查询所有子
     * 3、如果不是数采，则先查询父数采，然后通过父数采的id查询所有子parentId
     * @param deviceEntity
     * @return
     */
    @Override
    public DeviceEntity connection (DeviceEntity deviceEntity) {
        if ( deviceEntity == null ) {
            throw new StateException ( StateEnum.REQUEST_PARAM_EXCE );
        }
        if ( StringUtils.isBlank ( deviceEntity.getId () ) ) {
            throw new StateException ( StateEnum.PRIMARY_NULL_EXCE );
        }
        DeviceEntity device = baseMapper.selectById ( deviceEntity );
        if ( device == null ) {
            throw new StateException ( StateEnum.RESULT_NULL_EXCE );
        }
        if ( device.getDeviceType () == ConstUtil.DEVICE_TYPE_COLLECTOR ) {
            // 查询所有子
            List<DeviceEntity> deviceEntities = baseMapper.selectList ( new QueryWrapper<DeviceEntity> ().eq ( "parentId", device.getId () ) );
            device.setDeviceEntities ( deviceEntities );
        } else if ( device.getDeviceType () == ConstUtil.DEVICE_TYPE_INVERTER ) {
            // 如果不是数采，则先查询父数采，然后通过父数采的id查询所有子parentId
            DeviceEntity parentDevice = baseMapper.selectOne ( new QueryWrapper<DeviceEntity> ().eq ( "id", device.getParentId () ) );
            device = parentDevice;
            List<DeviceEntity> deviceEntities = baseMapper.selectList ( new QueryWrapper<DeviceEntity> ().eq ( "parentId", parentDevice.getId () ) );
            parentDevice.setDeviceEntities ( deviceEntities );
        }
        return device;
    }


    /**
     * 查询某逆变器直流电历史数据
     * @param deviceEntity
     * @return
     */
    @Override
    public Map<Object, Object> queryDcHistoryData (DeviceEntity deviceEntity) {
        DeviceEntity device = deviceMapper.querySingle ( deviceEntity );
        Map<Object, Object> map = new HashMap<> ();
        InverterDcAmountEntity inverterDcAmountEntity = new InverterDcAmountEntity ();
        inverterDcAmountEntity.setInverterSn ( device.getDeviceSn () );
        // 如果是今天
        if ( deviceEntity.getTimeType () == 1 ) {
            inverterDcAmountEntity.setStartTime ( deviceEntity.getStartTime () );
            inverterDcAmountEntity.setEndTime ( deviceEntity.getEndTime () );
            List<InverterDcAmountEntity> inverterDcAmountEntities = inverterDcAmountMapper.queryHistoryData ( inverterDcAmountEntity );
            map = parseDcAmountHistory ( inverterDcAmountEntities );
        } else if ( deviceEntity.getTimeType () == 2 ) {
            // 今天所在的这一周
            inverterDcAmountEntity.setStartTime ( deviceEntity.getStartTime () );
            inverterDcAmountEntity.setEndTime ( deviceEntity.getEndTime () );
            List<InverterDcAmountEntity> inverterDcAmountEntities = inverterDcAmountMapper.queryHistoryData ( inverterDcAmountEntity );
            map = parseDcAmountHistory ( inverterDcAmountEntities );
        }
        return map;
    }

    /**
     * 解析直流电发电历史数据，按时间节点组装
     * @param inverterDcAmountEntities
     * @return
     */
    private Map<Object, Object> parseDcAmountHistory (List<InverterDcAmountEntity> inverterDcAmountEntities) {
        Map<Object, Object> map = new HashMap<> ();
        Map<Date, List<InverterDcAmountEntity>> collect = inverterDcAmountEntities.stream ().collect ( Collectors.groupingBy ( InverterDcAmountEntity :: getCreateTime ) );
        for ( Map.Entry<Date, List<InverterDcAmountEntity>> entry : collect.entrySet () ) {
            map.put ( CommonUtil.getDateStrFormat ( entry.getKey (), "HH:mm:ss" ), entry.getValue () );
        }
        return map;
    }


    /**
     * 设备详情页
     * @param deviceEntity
     * @return
     */
    @Override
    public Map<String, Object> queryInverterDetails (DeviceEntity deviceEntity) {
        Map<String, Object> map = new HashMap<> ();
        if ( deviceEntity == null ) {
            throw new StateException ( StateEnum.REQUEST_PARAM_EXCE );
        }
        if ( StringUtils.isBlank ( deviceEntity.getId () ) ) {
            throw new StateException ( StateEnum.PRIMARY_NULL_EXCE );
        }
        DeviceEntity device = deviceMapper.querySingle ( deviceEntity );
        // 交流
        InverterAcAmountEntity inverterAcAmountEntity = new InverterAcAmountEntity ();
        inverterAcAmountEntity.setInverterSn ( device.getDeviceSn () );
        List<InverterAcAmountEntity> inverterAcAmountEntities = inverterAcAmountMapper.queryMaxDateList ( inverterAcAmountEntity );
        // 直流
        InverterDcAmountEntity inverterDcAmountEntity = new InverterDcAmountEntity ();
        inverterDcAmountEntity.setInverterSn ( device.getDeviceSn () );
        List<InverterDcAmountEntity> inverterDcAmountEntities = inverterDcAmountMapper.queryMaxDateList ( inverterDcAmountEntity );
        // 逆变器系统数据
        InverterElectricEntity inverterElectricEntity = new InverterElectricEntity ();
        inverterElectricEntity.setSerialNumber ( device.getDeviceSn () );
        inverterElectricEntity = inverterElectricMapper.queryMaxDate ( inverterElectricEntity );
        // 前一天发电量
        InverterAmountStatisticsDayEntity inverterAmountStatisticsDayEntity = new InverterAmountStatisticsDayEntity ();
        inverterAmountStatisticsDayEntity.setInverterSn ( device.getDeviceSn () );
        inverterAmountStatisticsDayEntity = inverterAmountStatisticsDayMapper.querySingle ( inverterAmountStatisticsDayEntity );
        String yesterdayPower = "0"; // 昨天发电量
        if ( inverterAmountStatisticsDayEntity != null ) {
            yesterdayPower = inverterAmountStatisticsDayEntity.getYesterdayPower ();
        }
        // 上月发电量
        double lastMonthPower = getMonthEnergy ( device.getDeviceSn (), - 1 );
        // 获取当月发电量
        double currentMonthPower = getMonthEnergy ( device.getDeviceSn (), 0 );
        // 当年发电量
        deviceEntity.setDeviceSn ( device.getDeviceSn () );
        double totalYearAmount = totalYearAmount ( deviceEntity );
        //直流输入总功率
        String allDCPower = "0", allActivePower = "0";
        for ( InverterDcAmountEntity dcAmountEntity : inverterDcAmountEntities ) {
            // 一台功率
            String power = CommonUtil.ride ( dcAmountEntity.getVoltage (), dcAmountEntity.getCurrent () );
            allDCPower = CommonUtil.sum ( allDCPower, power );
        }
        device.setAllDCPower ( allDCPower );
        //交流总功率
        for ( InverterAcAmountEntity acAmountEntity : inverterAcAmountEntities ) {
            // 一台功率
            String power = CommonUtil.ride ( acAmountEntity.getVoltage (), acAmountEntity.getCurrent () );
            allActivePower = CommonUtil.sum ( allActivePower, power );
        }
        device.setAllActivePower ( allActivePower );
        //历史总发电量
        device.setTotalEnergy ( inverterElectricEntity.getTotaEnergy () );
        //当日
        device.setTodayEnergy ( inverterElectricEntity.getTodayEnergy () );
        map.put ( "baseInfo", device );
        map.put ( "inverterElectricEntity", inverterElectricEntity );
        map.put ( "acList", inverterAcAmountEntities );
        map.put ( "dcList", inverterDcAmountEntities );
        map.put ( "yesterdayPower", yesterdayPower ); // 昨天
        map.put ( "lastMonthPower", CommonUtil.parseDecimalPoint ( lastMonthPower ) ); // 上个月
        map.put ( "currentMonthPower", CommonUtil.parseDecimalPoint ( currentMonthPower ) ); // 当月
        map.put ( "totalYearAmount", CommonUtil.parseDecimalPoint ( totalYearAmount ) );    // 当年总发电量
        return map;
    }


    /**
     * 查询逆变器的交流，月度，年度历史数据
     * 日、周查询交直流电流电压功率频率对比
     * 月、年、总查询发电量柱状图
     * @param deviceEntity
     * @return
     */
    @Override
    public Map<Object, Object> queryInverterHistoryData (DeviceEntity deviceEntity) {
        DeviceEntity device = deviceMapper.querySingle ( deviceEntity );
        Map<Object, Object> map = new HashMap<> ();
        InverterAcAmountEntity inverterAcAmountEntity = new InverterAcAmountEntity ();
        inverterAcAmountEntity.setInverterSn ( device.getDeviceSn () );
        InverterElectricEntity inverterElectricEntity = new InverterElectricEntity ();
        inverterElectricEntity.setSerialNumber ( device.getDeviceSn () );
        // 设置inverter_system_info分区名称
        LocalDate localDate = LocalDate.now ();
        String yearAndMonth = localDate.getYear () + "" + ( localDate.getMonthValue () < 10 ? "0" + localDate.getMonthValue () : localDate.getMonthValue () );
        String partitionName = "partition (p" + yearAndMonth + ")";
        //inverterSystemEntity.setPartitionName ( partitionName );
        // 该逆变器今天的发电数据
        InverterElectricEntity inverterToday = inverterElectricMapper.queryInverterTodayMax ( inverterElectricEntity );
        // 如果是今天
        if ( deviceEntity.getTimeType () == 1 ) {
            inverterAcAmountEntity.setStartTime ( deviceEntity.getStartTime () );
            inverterElectricEntity.setStartTime ( deviceEntity.getStartTime () );
            inverterAcAmountEntity.setEndTime ( deviceEntity.getEndTime () );
            inverterElectricEntity.setEndTime ( deviceEntity.getEndTime () );
            List<InverterAcAmountEntity> inverterAcAmountEntities = inverterAcAmountMapper.queryHistoryData ( inverterAcAmountEntity );
            List<InverterElectricEntity> inverterSystemEntities = inverterElectricMapper.queryHistoryData ( inverterElectricEntity );
            map = parseAcAmountHistory ( inverterAcAmountEntities );
            map.put ( "allActivePower", inverterSystemEntities );
        } else if ( deviceEntity.getTimeType () == 2 ) {
            // 前端指定一周的时间
            inverterAcAmountEntity.setStartTime ( deviceEntity.getStartTime () );
            inverterElectricEntity.setStartTime ( deviceEntity.getStartTime () );
            inverterAcAmountEntity.setEndTime ( deviceEntity.getEndTime () );
            inverterElectricEntity.setEndTime ( deviceEntity.getEndTime () );
            List<InverterAcAmountEntity> inverterAcAmountEntities = inverterAcAmountMapper.queryHistoryData ( inverterAcAmountEntity );
            List<InverterElectricEntity> inverterSystemEntities = inverterElectricMapper.queryHistoryData ( inverterElectricEntity );
            map = parseAcAmountHistory ( inverterAcAmountEntities );
            map.put ( "allActivePower", inverterSystemEntities );
        } else if ( deviceEntity.getTimeType () == 3 ) {
            // 查询该逆变器本月发电量
            InverterAmountStatisticsDayEntity inverterAmountStatisticsDayEntity = new InverterAmountStatisticsDayEntity ();
            inverterAmountStatisticsDayEntity.setInverterSn ( device.getDeviceSn () );
            inverterAmountStatisticsDayEntity.setStartTime ( deviceEntity.getStartTime () );
            inverterAmountStatisticsDayEntity.setEndTime ( deviceEntity.getEndTime () );
            List<InverterAmountStatisticsDayEntity> inverterAmountStatisticsDayEntities = inverterAmountStatisticsDayMapper.queryHistoryList ( inverterAmountStatisticsDayEntity );
            for ( InverterAmountStatisticsDayEntity amountStatisticsDayEntity : inverterAmountStatisticsDayEntities ) {
                map.put ( CommonUtil.getDateStrFormat ( amountStatisticsDayEntity.getCollectorTime (), "yyyy-MM-dd" ), amountStatisticsDayEntity.getYesterdayPower () );
            }
            if ( inverterToday != null ) {
                if ( CommonUtil.checkMonth ( CommonUtil.getDateStrFormat ( deviceEntity.getStartTime (), "yyyy-MM" ) ) ) {
                    map.put ( CommonUtil.getDateStrFormat ( new Date (), "yyyy-MM-dd" ), inverterToday.getTodayEnergy () );
                }
            }
        } else if ( deviceEntity.getTimeType () == 4 ) {
            InverterAmountStatisticsDayEntity inverterAmountStatisticsDayEntity = new InverterAmountStatisticsDayEntity ();
            inverterAmountStatisticsDayEntity.setInverterSn ( device.getDeviceSn () );
            inverterAmountStatisticsDayEntity.setStartTime ( deviceEntity.getStartTime () );
            inverterAmountStatisticsDayEntity.setEndTime ( deviceEntity.getEndTime () );
            List<InverterAmountStatisticsDayEntity> inverterAmountStatisticsDayEntities = inverterAmountStatisticsDayMapper.queryHistoryList ( inverterAmountStatisticsDayEntity );
            Map<String, List<InverterAmountStatisticsDayEntity>> collect = inverterAmountStatisticsDayEntities.stream ().collect ( Collectors.groupingBy ( InverterAmountStatisticsDayEntity :: getMonthTime ) );
            for ( Map.Entry<String, List<InverterAmountStatisticsDayEntity>> stringListEntry : collect.entrySet () ) {
                double sumByDay = stringListEntry.getValue ().stream ().mapToDouble ( allPower -> Double.parseDouble ( allPower.getYesterdayPower () ) ).sum ();
                map.put ( stringListEntry.getKey (), CommonUtil.parseDecimalPoint ( sumByDay ) );
            }
            // 查询年份如果是今年，则加上今天的
            if ( inverterToday != null ) {
                Object currentMonth = map.get ( CommonUtil.getDateStrFormat ( new Date (), "yyyy-MM" ) );
                if ( CommonUtil.checkYear ( CommonUtil.getDateStrFormat ( deviceEntity.getStartTime (), "yyyy" ) ) ) {
                    double currentPower = Double.parseDouble ( String.valueOf ( currentMonth == null ? "0" : currentMonth ) );
                    map.put ( CommonUtil.getDateStrFormat ( new Date (), "yyyy-MM" ), CommonUtil.parseDecimalPoint ( currentPower + Double.parseDouble ( inverterToday.getTodayEnergy () ) ) );
                }
            }
        }
        return map;
    }

    /**
     * 获取今年全年截止到现在的发电量之和=月份统计表之和 + 日统计表本月的天数之后
     * @param deviceEntity
     */
    private double totalYearAmount (DeviceEntity deviceEntity) {
        InverterAmountStatisticsDayEntity inverterAmountStatisticsDayEntity = new InverterAmountStatisticsDayEntity ();
        inverterAmountStatisticsDayEntity.setInverterSn ( deviceEntity.getDeviceSn () );
        // 今年的所有月份
        Calendar calendar = Calendar.getInstance ();
        calendar.set ( Calendar.MONTH, 0 );//1月
        calendar.set ( Calendar.DAY_OF_MONTH, 1 );
        inverterAmountStatisticsDayEntity.setStartTime ( calendar.getTime () );
        calendar.set ( Calendar.MONTH, 11 );//12月
        int actualMaximum = calendar.getActualMaximum ( Calendar.DATE );
        calendar.set ( Calendar.DAY_OF_MONTH, actualMaximum );
        double monthPower = 0;
        inverterAmountStatisticsDayEntity.setEndTime ( calendar.getTime () );
        List<InverterAmountStatisticsDayEntity> inverterAmountStatisticsDayEntities = inverterAmountStatisticsDayMapper.queryHistoryList ( inverterAmountStatisticsDayEntity );
        for ( InverterAmountStatisticsDayEntity amountStatisticsDayEntity : inverterAmountStatisticsDayEntities ) {
            monthPower += Double.parseDouble ( amountStatisticsDayEntity.getYesterdayPower () );
        }
        return monthPower;
    }


    /**
     * 解析交流发电数据，按时间节点组装，返给历史数据
     * @param inverterAcAmountEntities
     * @return
     */
    private Map<Object, Object> parseAcAmountHistory (List<InverterAcAmountEntity> inverterAcAmountEntities) {
        Map<Object, Object> map = new HashMap<> ();
        Map<Date, List<InverterAcAmountEntity>> collect = inverterAcAmountEntities.stream ().collect ( Collectors.groupingBy ( InverterAcAmountEntity :: getCreateTime ) );
        for ( Map.Entry<Date, List<InverterAcAmountEntity>> entry : collect.entrySet () ) {
            map.put ( CommonUtil.getDateStrFormat ( entry.getKey (), "HH:mm:ss" ), entry.getValue () );
        }
        return map;
    }


    /**
     * 获取当月或本月发电量数据
     * @param sn：逆变器SN
     * @param monthOffset：月份偏移，正数加月份，负数减月份，0表示当月
     * @return
     */
    private double getMonthEnergy (String sn, int monthOffset) {
        Date currentDate = new Date ();
        Calendar calendar = Calendar.getInstance ();
        calendar.setTime ( currentDate );
        // 将日期设置为上个月
        calendar.add ( Calendar.MONTH, monthOffset );
        // 获取上个月的日期
        Date lastMonthDate = calendar.getTime ();
        InverterAmountStatisticsDayEntity lastMonthDayEntity = new InverterAmountStatisticsDayEntity ();
        lastMonthDayEntity.setInverterSn ( sn );
        lastMonthDayEntity.setMonthTime ( CommonUtil.getDateStrFormat ( lastMonthDate, "yyyy-MM" ) );
        List<InverterAmountStatisticsDayEntity> inverterAmountStatisticsDayEntities = inverterAmountStatisticsDayMapper.queryLastMonth ( lastMonthDayEntity );
        double sum = 0;
        if ( inverterAmountStatisticsDayEntities != null && inverterAmountStatisticsDayEntities.size () > 0 ) {
            sum = inverterAmountStatisticsDayEntities.stream ().mapToDouble ( allPower -> Double.parseDouble ( allPower.getYesterdayPower () ) ).sum ();
        }
        return sum;
    }

}
